import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { numberToChinese, STData,STRes } from '@delon/abc';
import { UtilService } from '@core/services/util.service';

@Component({
  selector: 'app-pay-static-pay-list',
  templateUrl: './pay-list.component.html',
})
export class PayStaticPayListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  showTable:boolean=false;
  config: SPage = {
    columns: [
      {
        index: 'days',
        title: '日期',
        width:200,
        type:ColumnType.tag,
        filter: {
          index:"communityId",
          type:'select',
          placeholder:"所属社区",
          items:[]
        }
      },
      {
        index: 'payDeposit',
        title: '押金(收入/元)',
        fFn:(item,column)=>{
          return item.payDeposit / 100 + "";
        },
        type:ColumnType.tag,
        statistical: 'sum',
        key: 'payDeposit',
        filter: {
          index:"areaId",
          placeholder: '所属小区',
          type: 'select',
          items:[]
        }
      },
      {
        index: 'payRent',
        title: '卡费(收入/元)',
        fFn:(item,column)=>{
          return item.payRent / 100 + "";
        },
        type:ColumnType.tag,
        statistical: 'sum',
        key: 'payRent',
        // filter: {
        //   placeholder: '支付类型',
        //   type: 'select',
        //   items:[
        //   ]
        // }
      },
      {
        index: 'inStatic',
        title: '收入合计(元)',
        fFn:(item,column)=>{
          let pay1=Number(item.payDeposit);
          let pay2=Number(item.payRent);
          return (pay1 + pay2) / 100 +"" ;
        },
        type:ColumnType.tag,
        statistical: 'sum',
        key: 'inStatic',
        filter: {
          index:"startDate",
          placeholder: '支付时间(起)',
          type: 'date'
        }
      },
      {
        index: 'payoutDeposit',
         title: '押金(支出/元)',
         fFn:(item,column)=>{
          return item.payoutDeposit / 100 + "";
        },
         type:ColumnType.tag,
         statistical: 'sum',
         key: 'payoutDeposit',
         filter: {
           index:"endDate",
           placeholder: '支付时间(止)',
           type: 'date'
        }
      },
      {
        index: 'payoutRent',
        title: '卡费(支出/元)',
        fFn:(item,column)=>{
          return item.payoutRent / 100 + "";
        },
        type:ColumnType.tag,
        statistical: 'sum',
        key: 'payoutRent',
        filter: {
          index:"createBy",
          placeholder: '办理人',
          type: 'string'
        }
      },
      {
         index: 'outStatic',
         title: '支出合计(元)',
         fFn:(item,column)=>{
           let pay1=Number(item.payoutDeposit);
           let pay2=Number(item.payoutRent);
          return (pay1 + pay2) / 100 + "" ;
        },
         type:ColumnType.tag,
         statistical: 'sum',
         key: 'outStatic',
         filter:{
            index:"createId",
            type:ColumnType.checkbox,
            items:[
              {label:"只统计当前账号产生的费用",value:"1"}
            ]
         }
      },
      {
         index: 'allStatic',
         title: '收支结余(元)',
         fFn:(item,column)=>{
           let pay1=Number(item.payDeposit);
           let pay2=Number(item.payRent);
           let pay3=Number(item.payoutDeposit);
           let pay4=Number(item.payoutRent);
          return (pay1 + pay2 + pay3 + pay4) / 100 +"";
        },
         type:ColumnType.tag,
         statistical: 'sum',
         key: 'allStatic',
      },
    ],
    showCheckBox: false,
    reqUrl:this.api.pay.incomeStatistics.url,
    defaultParam:{"statisticsType":"1","userType":JSON.parse(this.util.getStorage("userNow")).userType}
    //data:[]
    // editBtn: [
    //   { title: '编辑' }
    // ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private util:UtilService) { }

  ngOnInit() { 
    //this.getData();
    this.getArea().then((res)=>{
      this.feachCommunity();
    })
  }

  dataProcess(data: STData[]){
    console.log(data);
    return data;
  }

  getData(){
    this.http.post(this.api.pay.payList.url,{}).subscribe((res)=>{
      console.log(res);
      this.config.data=res.data;
    })
  }
  _console(e: any): void {
    this.selectData = e;
  }
   //获取小区下拉框数据
   feachCommunity(){
    this.http.post(this.api.area.list.url, {areaName: '',areaRank:6}).subscribe((res: any) => {
     if (res.status == '200') {
       this.config.columns[1].filter.items= res.data.map(item => {
         return {
           label: item.areaName,
           value: item.id
         }
       });
       this.showTable=true;
     }
   })
 }
 //获取社区下拉框数据
 getArea(){
  return new Promise<void>((resolve) => {
    this.http.post(this.api.area.page.url, {areaRank: '5'}).subscribe((res: any) => {
      console.log(res)
      if (res.status == '200') {
        if(res.data.list.length > 0){
          this.config.columns[0].filter.items= res.data.list.map(item => {
            return {
              label: item.areaName,
              value: item.id
            }
          });
        }
        resolve();
      }
    })
  }) 
 }

  //导出
  export(){
    this.loading=true;
    let body:any={};
    if(this.st.st.req.params != undefined){
      body=this.st.st.req.params;
    }
    body.pageNum=1;
    body.pageSize=this.st.st.total;
    this.http.post(this.api.pay.incomeStatistics.url,body).subscribe(res => {
      this.loading=false;
      if(res.status == 200 && res.data.list.length > 0){
        let data=res.data.list;
         this.st.st.export(data,{
          filename : "收支统计.xlsx"
        })
      }else{
        this.message.error(res.message);
      }
    })
  }

   //按日统计
   staticByDay(){
    let res=this.getParams(this.st.sf.value);
    this.st.st.reset({"statisticsType":"1",...res});
  }
  //按月统计
  staticByMonth(){
    let res=this.getParams(this.st.sf.value);
    this.st.st.reset({"statisticsType":"0",...res});
  }

  getParams(res){
    let result=res;
    let data:any=this.util.getStorage("userNow");
    data=JSON.parse(data);
    Object.keys(result).forEach(item=>{
      if(Array.isArray(result[item])){
        if(result[item].length != 0){
          result[item]=data.id;
        }else{
          delete result[item];
        }
      }
    })
    return result;
  }
}
